package com.itcast.demo_one

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.itcast.demo_one.fragment.DetailFragment
import com.itcast.demo_one.fragment.ListFragment


class MainActivity : AppCompatActivity(), ListFragment.OnItemClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        // 调用父类的onCreate方法，确保基础初始化完成
        super.onCreate(savedInstanceState)
        
        // 启用边缘到边缘显示效果，让应用界面延伸到状态栏和导航栏区域
         enableEdgeToEdge()
        
        // 设置当前Activity要显示的布局文件
        setContentView(R.layout.activity_main)

        // 加载Fragment
        // 如果 savedInstanceState == null，则说明当前Activity是第一次创建，需要加载Fragment
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.fragment_container, ListFragment())
                .commit()
        }

    }

    override fun onItemClick(position: Int) {
       // Toast.makeText(this, "点击了第 $position 项", Toast.LENGTH_SHORT).show()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, DetailFragment()) // 替换Fragment
            .addToBackStack(null) // 添加到返回栈
            .commit() // 提交事务
    }
}